package com.coyee.stream.converter;

import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.*;
import java.io.File;

public class H265toH264 {

    public boolean isH265(String filePath) {
        FFmpegFrameGrabber grabber = null;
        try {
            grabber = new FFmpegFrameGrabber(filePath);
            grabber.start();
            int videoCodec = grabber.getVideoCodec();

            if (videoCodec == avcodec.AV_CODEC_ID_H264) {
                System.out.println(filePath + "文件编码格式为 H.264");
            } else if (videoCodec == avcodec.AV_CODEC_ID_H265) {
                System.out.println(filePath + "文件编码格式为 H.265");
                return true;
            } else {
                System.out.println(filePath + "videoCodec = " + videoCodec);
            }

            grabber.stop();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                grabber.stop();
            } catch (FrameGrabber.Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public void convertH264(String inputPath, String inputFileName, String outputPath, String outputFileName) {

        FFmpegFrameGrabber grabber = null;
        FFmpegFrameRecorder recorder = null;
        try {
            /*
            if (!isH265(input)) {
                return;
            }
            */
            /*
            // 先改名为 .bak
            boolean isRename = new File(input).renameTo(new File(input + ".h265"));
            if (!isRename) {
                System.out.println("备份H265文件失败");
                return;
            }
            */


            File h265 = new File(inputPath + File.separator + inputFileName);
            if (h265 == null || !h265.isFile()) {
                return;
            }
            File outputDir = new File(outputPath);
            if (outputDir == null) {
                return;
            }
            if (!outputDir.exists()) {
                outputDir.mkdirs();
            }
            if (!outputDir.exists()) {
                return;
            }
            File h264 = new File(outputPath + File.separator + outputFileName);

            Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
            grabber = new FFmpegFrameGrabber(h265);
            grabber.start();

            recorder = new FFmpegFrameRecorder(h264, grabber.getImageWidth()/2, grabber.getImageHeight()/2, grabber.getAudioChannels());
            recorder.setFormat("ts");
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
            recorder.setFrameRate(25);
            recorder.setGopSize(25);
            recorder.setVideoBitrate(grabber.getVideoBitrate());
            recorder.start();

            Frame frame = null;
            while ((frame = grabber.grabFrame()) != null) {
                recorder.record(frame);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (grabber != null) {
                try {
                    grabber.stop();
                } catch (FrameGrabber.Exception e) {
                    e.printStackTrace();
                }
            }
            if (recorder != null) {
                try {
                    recorder.stop();
                } catch (FrameRecorder.Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) throws Exception {

        H265toH264 h265toH264 = new H265toH264();
        h265toH264.convertH264("C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play101.ts.h264", "C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play101.mp4");
        h265toH264.convertH264("C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play102.ts.h264", "C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play102.mp4");
        h265toH264.convertH264("C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play103.ts.h264", "C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play103.mp4");
        h265toH264.convertH264("C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play104.ts.h264", "C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play104.mp4");
        h265toH264.convertH264("C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play105.ts.h264", "C:\\dev\\workspace\\JinAn\\TJMonitor-rtsp-web-converter-hls\\target", "play105.mp4");



    }
}